<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 *
 * @author Joker
 * id int(10) NOT NULL AUTO_INCREMENT COMMENT '编号',
	user_id int(10) not NULL COMMENT '用户编号',
	app VARCHAR(15) NOT NULL COMMENT '所属表名',
	app_id int(10) NOT NULL COMMENT '资源ID',
	chapter_id int(10) NOT NULL DEFAULT 0 COMMENT '当前章节编号',
	content_id  int(10) NOT NULL DEFAULT 0 COMMENT '当前小节编号',
	chapter_num TINYINT(2) NOT NULL DEFAULT 0 COMMENT '当前学习章节',
	chapters TINYINT(2) NOT NULL DEFAULT 0 COMMENT '课程总章节',
	price MEDIUMINT(6) NOT NULL DEFAULT 0 COMMENT '所花积分',
	finished TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否已学完',
	finished_at datetime COMMENT '完成时间',
	created_at datetime DEFAULT CURRENT_TIMESTAMP COMMENT '开始学习时间',
	updated_at datetime DEFAULT CURRENT_TIMESTAMP COMMENT '再次学习时间',
	deleted_at datetime COMMENT '删除学习课程时间',       
 */
class UserStudy extends Model
{
    protected $table='user_study';
    
    protected $primaryKey = 'id';
    
    protected $fillable =['id','user_id','app','app_id','chapter_id','content_id','chapter_num','chapters','price','finished'];
    
    protected $dates = ['deleted_at','finished_at'];
    
    /**
     * 获取学习列表
     */
    public function getList($user_id,$sort)
    {
//        return DB::table('teaches as t')
//                ->leftJoin('user_study as us','us.app_id','=','t.id')
//                ->leftJoin('teaches_chapter as tcpt','tcpt.id','=','us.chapter_id')
//                ->leftJoin('teaches_content as tctt','tctt.id','=','us.content_id')
//                ->leftJoin('users as u','t.user_id','=','u.id')
//                ->leftJoin('attachment as a','t.thumb','=','a.aid')
//                ->leftJoin('category as c','t.cat_id','=','c.cat_id')
//                ->select('us.*','tcpt.name as tcname','tctt.name as tccname','u.name as username','t.name as ttname','t.id as tid','t.price as tprice','t.views_num','t.comments_num','t.studys_num','t.attention_num','t.level','t.created_at as ttime','a.url as thumb','c.cat_name')
//                ->where(array('us.user_id'=>$user_id,'us.app'=>'teaches'))
//                ->orderBy('us.created_at','desc')
//                ->orderBy('us.finished_at','desc')
//                ->paginate(10);

        $query = DB::table('teaches as t')
                ->leftJoin('user_study as us','us.app_id','=','t.id')
                ->leftJoin('teaches_chapter as tcpt','tcpt.id','=','us.chapter_id')
                ->leftJoin('teaches_content as tctt','tctt.id','=','us.content_id')
                ->leftJoin('users as u','t.user_id','=','u.id')
                ->leftJoin('attachment as a','t.thumb','=','a.aid')
                ->leftJoin('category as c','t.cat_id','=','c.cat_id')
                ->select('us.*','tcpt.name as tcname','tctt.name as tccname','u.name as username','t.name as ttname','t.id as tid','t.price as tprice','t.views_num','t.comments_num','t.studys_num','t.attention_num','t.level','t.created_at as ttime','a.url as thumb','c.cat_name')
                ->where(array('us.user_id'=>$user_id,'us.app'=>'teaches'));
        if($sort){
            switch ($sort){
                case 1:
                    $query->orderBy('t.views_num','desc');break;
                case 2:
                    $query->orderBy('t.comments_num','desc');break;
                default:
                    $query->orderBy('t.created_at','desc');

            }
            $result = $query->paginate(10);
        }else{
            $result = $query->orderBy('us.created_at','desc')
                            ->orderBy('us.finished_at','desc')
                            ->paginate(10);
        }

        return $result;
    }
    
}